home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / QFLOAT / QTANH.C < prev    next >
C/C++ Source or Header  |  1996-02-25  |  1KB  |  62 lines

  1. /*                            qtanh.c        */
  2. /* hyperbolic tangent check routine */
  3. /* this subroutine is used by the exponential function routine */
  4.  
  5. /* loop count adjusted for convergence to 9 word mantissa if x <= 2 */
  6.  
  7. #include "qhead.h"
  8.  
  9. extern QELT qone[], qtwo[];
  10.  
  11. int qtanh( x, y )
  12. QELT *x, *y;
  13. {
  14. QELT e[NQ], r[NQ], j[NQ], xx[NQ], m2[NQ];
  15. int i, n;
  16. long lj;
  17.  
  18. qmov( x, r );
  19. r[0] = 0;
  20. if( qcmp(r, qone) >= 0 )
  21.     {
  22. /* tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
  23.  * Note qexp() calls qtanh, but with an argument less than (1 + log 2)/2.
  24.  */
  25.     qexp( r, e );
  26.     qdiv( e, qone, r );
  27.     qsub( r, e, xx );
  28.     qadd( r, e, j );
  29.     qdiv( j, xx, y );
  30.     goto done;
  31.     }
  32.  
  33. qmov( qtwo, m2 );
  34. qneg( m2 );
  35.  
  36. n = NBITS/9; /*10;*/
  37. lj = 2 * n + 1;
  38. ltoq( &lj, j );
  39.  
  40. qmov( j, e );
  41. qmul( x, x, xx );
  42.  
  43. /* continued fraction */
  44.  
  45. /*for( i=0; i<15; i++)*/
  46.  
  47.  
  48. for( i=0; i<n; i++)
  49.     {
  50.     qdiv( e, xx, r );
  51.     qadd( m2, j, j );
  52.     qadd( r, j, e );
  53.     }
  54.  
  55. qdiv( e, x, y );
  56.  
  57. done:
  58. if( x[0] != 0 )
  59.     y[0] = -1;
  60. return 0;
  61. }
  62.